<?php

class Modules_Clubs extends Module {

	public $name = 'clubs';
	public $title = "Клубы";

	protected function _actionproperties(Blocks $block) {
		switch ($block->action) {
			case "club_list":

				return $this->_club_list($block);

				break;
		}
	}

	public function actions() {
		return array(
			'club_list' => 'Список клубов'
		);
	}

	public function mainActions() {
		return array(
			'club_list' => 'Список клубов'
		);
	}

	public static function subMenu() {
		return array(
			array(
				'icon' => '/tpl/admin/images/card.png',
				'url' => '/admin.php?module=clubs&action=clubs_list',
				'title' => 'Список клубов',
			),
			array(
				'icon' => '/tpl/admin/images/folder_card.png',
				'url' => '/admin.php?module=clubs&action=categories_list',
				'title' => 'Категории клубов',
			),
		);
	}

	public function adminActionIndex() {
		return $this->adminActionClubs_List();
	}

	public function adminActionClubs_List() {
		$this->access("r");
		
		$clubs = Doctrine_Query::create()
				->from('Clubs c')
				->leftJoin('c.Category cat')
				->leftJoin('c.Owner')
				->leftJoin('c.Administration')
				->leftJoin('c.Members')
				->leftJoin('cat.ParentCategory')
				->execute();

		return $this->renderTpl('clubs_list.html', array(
			'clubs' => $clubs
		));
	}

	public function adminActionClubs_create() {
		$this->access("c");
		
		$club = new Clubs();
		return $this->_edit_club($club, 'club_create.html');
	}

	public function adminActionClubs_edit() {
		$this->access("c");
		
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);

		$club = Doctrine_Query::create()
				->from("Clubs c")
				->where('c.id = ?', $id)
				->fetchOne();
		if (!$club) Request::redirect(404);

		return $this->_edit_club($club, 'club_edit.html');
	}

	public function adminActionClubs_save() {
		$this->access("c");
		
		$clubData = isset($_REQUEST['club']) ? $_REQUEST['club'] : array();
		$clubData['id'] = isset($clubData['id']) ? (int) $clubData['id'] : 0;

		$club = $clubData['id'] ? ClubsTable::getInstance()->find($clubData['id']) : new Clubs();
		unset($clubData['id']);
		$club->fromArray($clubData);
		$club->save();

		Request::redirect(isset($_REQUEST['apply']) ? "?module=clubs&action=club_edit&id={$club->id}" : "?module=clubs&action=clubs_list");
	}

	public function adminActionCategories_list() {
		$this->access("r");
		
		$parent = (!empty($_REQUEST['parent'])) ? (int) $_REQUEST['parent'] : 1;
		if ($parent > 1) {
			$parentCat = Doctrine_Query::create()
					->from("ClubsCategories parent")
					->where('parent.id = ?', $parent)
					->fetchOne();
		} else {
			$parentCat = false;
		}

		$categories = Doctrine_Query::create()
				->from("ClubsCategories cats")
				->where('cats.parent = ?', $parent)
				->andWhere('cats.id != ?', 1)
				->execute();

		return $this->renderTpl('categories_list.html', array(
			'categories' => $categories,
			'parent' => $parentCat,
		));
	}

	public function adminActionCategories_save() {
		$this->access("c");
		
		$categoryData = isset($_REQUEST['category']) ? $_REQUEST['category'] : array();
		$categoryData['id'] = isset($categoryData['id']) ? $categoryData['id'] : 0;

		$category = $categoryData['id'] ? ClubsCategoriesTable::getInstance()->find($categoryData['id']) : new ClubsCategories();
		unset($categoryData['id']);
		$category->fromArray($categoryData);
		$category->save();

		if (App::$XMLHttpRequest) return $this->renderTpl('_category.html', array('category' => $category));

		Request::redirect('?module=clubs&action=categories_list&parent=' . $category->parent);
	}

	public function adminActionCategories_delete() {
		$this->access("d");
		
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);

		$category = Doctrine_Query::create()
				->from('ClubsCategories cat')
				->where('cat.id = ?', $id)
				->fetchOne();
		if (!$category) Request::redirect(404);

		$category->delete();

		if (App::$XMLHttpRequest) return 1;
		Request::redirect('?module=clubs&action=categories_list&parent=' . $category->parent);
	}
	
	public function actionClub_list(){
		$category = $this->block->property('category')->value;
		$clubs = Doctrine_Query::create()
				->select('c.*, cat.*, o.*, pcat.*, COUNT(m.id) AS members_count')
				->from("Clubs c")
				->leftJoin('c.Category cat')
				->leftJoin('c.Owner o')
				->leftJoin('c.Members m')
				->leftJoin('cat.ParentCategory pcat')
				->execute();
		
		$this->renderTpl('', array('clubs' => $club));
	}

	private function _edit_club($club, $tpl) {
		$categories = Doctrine_Query::create()
				->from('ClubsCategories cat')
				->leftJoin("cat.SubCategories subcat")
				->where('cat.id != 1')
				->andWhere('cat.parent = 1')
				->orderBy('cat.title, subcat.title')
				->execute();

		$users = Doctrine_Query::create()
				->from("Users u")
				->orderBy('u.login')
				->execute();

		return $this->renderTpl($tpl, array(
			'users' => $users,
			'categories' => $categories,
			'club' => $club,
		));
	}

	private function _club_list(Blocks $block) {
		$cats = Doctrine_Query::create()
				->from("ClubsCategories cat")
				->leftJoin("cat.SubCategories subcat")
				->where('cat.id != 1')
				->andWhere('cat.parent = 1')
				->orderBy('cat.title, subcat.title')
				->execute();
		return $this->renderTpl('_club_list.html', array('categories' => $cats));
	}

}